<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://unpkg.com/vue@next"></script>
</head>

<body>
    <div id="app"></div>
</body>
<script>
    let demo = {
        template: `<div>demo</div>`,
        created () { console.log('new demo'); },
        //当使用了keep-alive时出现的周期，因为keep-alive有缓存性，切换组建时不会销毁组件
        //进入组件时的钩子
        activated () { console.log('demo back'); },
        //离开组件时的钩子 
        deactivated () { console.log('demo gone'); }
    }
    let ipt = {
        template: `<div>ipt</div>`,
        created () { console.log('new ipt'); },
        activated () { console.log('ipt back'); },
        deactivated () { console.log('ipt gone'); }
    }
    let vm = {
        components: { demo, ipt },
        template: `
                    <div>
                        <keep-alive>
                            <component :is="currentItem" />
                        </keep-alive>
                        <button @click="handler">切换组件</button>
                    </div>
                `,
        data () {
            return {
                currentItem: 'ipt'
            }
        },
        methods: {
            handler () {
                if (this.currentItem == 'demo') {
                    this.currentItem = 'ipt'
                } else {
                    this.currentItem = 'demo'
                }
            }
        }

    }
    Vue.createApp(vm).mount('#app')
</script>

</html>